cat(sprintf("\nScript 4: Produce tables (%s)\n", f_time_gmt()))

# Load functions ---------------------------------------------------------------

options(tinytable_latex_placement = "t")

# functions to format numbers
fs <- \(x) formatC(x, digits = 3, format = "f")
fb <- \(x) formatC(x, digits = 0, big.mark = ",", format = "f")

# function to produce GOF elements in tables
f_gof <- \(model){
  data.table(
    "N" = nobs(model) |> fb(),
    "N Respondents" = length(unique(fixest_data(model, "estimation")$r_id)) |> fb(),
    "Adj. R2" = r2(model)[["ar2"]] |> fs(),
    "Std. Errors" = "By Resp." 
  )
}
my_gofs <- c("N", "N Respondents", "Adj. R2", "Std. Errors")




# Load data --------------------------------------------------------------------

# Load data from original studies
cint <- readRDS(here("proc", "cint.rds"))
bovitz <- readRDS(here("proc", "bovitz.rds"))

# Load data from H&R
hr_raw <- readRDS(here("proc", "hr.rds"))
# (remove others and independents following H&R)
hr <- copy(hr_raw)[!party %in% c("Other", "Independent"),]




# Table 1: original results and replications -----------------------------------

# Estimate baseline models
m_baseline_hr <- feols(newdv ~ treatment, hr, cluster = ~r_id)
m_baseline_cint <- feols(newdv ~ treatment, cint, cluster = ~r_id)
m_baseline_bovitz <- feols(newdv ~ treatment, bovitz, cluster = ~r_id)

# Estimate education interaction models
m_ed_hr <- feols(newdv ~ treatment * education_num, hr, cluster = ~r_id)
m_ed_cint <- feols(newdv ~ treatment * education_num, cint, cluster = ~r_id)
m_ed_bovitz <- feols(newdv ~ treatment * education_num, bovitz, cluster = ~r_id)

# Gather models as list
l_m_t1 <- list(m_baseline_hr, m_baseline_cint,m_baseline_bovitz,
               m_ed_hr, m_ed_cint, m_ed_bovitz)

# Produce regression table using modelsummary
tt_t1 <- msummary(l_m_t1,
                  stars = c('***' = .01, '**' = .05, '*' = .1), 
                  coef_map = c("treatment" = "Pos. key",
                               "treatment:education_num" = "Pos. key x Educ.",
                               "education_num" = "Educ.",
                               "(Intercept)" = "(Intercept)"),
                  gof_function = f_gof, gof_map = my_gofs, output = "gt") |>
  tab_spanner("Baseline", columns = 2:4) |> 
  tab_spanner("Education interaction", columns = 5:7) |> 
  tab_style(style = cell_fill(color = "#ffffe0"),
            locations = cells_body(columns = 2:4, rows = 1:2)) |> 
  tab_style(style = cell_fill(color = "#ffffe0"),
            locations = cells_body(columns = 5:7, rows = 3:4)) |> 
  cols_label(
    "(1)" = latex("\\makecell{(1A) \\\\ H\\&R}"),
    "(2)" = latex("\\makecell{(1B) \\\\ Lucid/Cint}"),
    "(3)" = latex("\\makecell{(1C) \\\\ Bovitz}"),
    "(4)" = latex("\\makecell{(2A) \\\\ H\\&R}"),
    "(5)" = latex("\\makecell{(2B) \\\\ Lucid/Cint}"),
    "(6)" = latex("\\makecell{(2C) \\\\ Bovitz}")
  ) |> 
  tab_caption(latex("Original results and replications\\label{tbl-main}"))

# Save table to disk
fp_out_t1 <- here("tables", "table_1_main_results.tex")
gtsave(tt_t1, fp_out_t1)

# Load .tex file and implement ad-hoc fixes
lines_out <- readLines(fp_out_t1)
## properly render "<"
lines_out <- lines_out |> str_replace_all("<", latex("$<$"))
## insert \midrule before N
which_n <- str_which(lines_out, "^N \\&")
lines_out <- c(lines_out[1:(which_n-1)], 
               "\\midrule",
               lines_out[which_n:length(lines_out)])

# Save final version of .tex file
cat(sprintf("--Save tables/table_1_main_results.tex (%s)\n", f_time_gmt()))
lines_out |> writeLines(fp_out_t1)



# Table 2: results by attention check status -----------------------------------

# Estimate Lucid/Cint models
m_ed_cint_none <- feols(newdv ~ treatment * education_num, cint[mock_count==0,],
                        cluster = ~r_id)
m_ed_cint_some <- feols(newdv ~ treatment * education_num, cint[mock_count %in% 1:2,],
                        cluster = ~r_id)
m_ed_cint_all <- feols(newdv ~ treatment * education_num, cint[mock_count==3],
                       cluster = ~r_id)

# Estimate Bovitz models
m_ed_bovitz_none <- feols(newdv ~ treatment * education_num, bovitz[mock_count==0,],
                          cluster = ~r_id)
m_ed_bovitz_some <- feols(newdv ~ treatment * education_num, bovitz[mock_count %in% 1:2,],
                          cluster = ~r_id)
m_ed_bovitz_all <- feols(newdv ~ treatment * education_num, bovitz[mock_count==3],
                         cluster = ~r_id)

# Gather models as list
l_m_t2 <- list(m_ed_cint_none, m_ed_cint_some, m_ed_cint_all,
               m_ed_bovitz_none, m_ed_bovitz_some, m_ed_bovitz_all)

# Produce regression table using modelsummary
tt_t2 <- msummary(l_m_t2,
                  gof_function = f_gof, gof_map = my_gofs,
                  stars = c('***' = .01, '**' = .05, '*' = .1),
                  coef_map = c("treatment" = "Pos. key",
                               "treatment:education_num" = "Pos. key x Educ.",
                               "education_num" = "Educ.",
                               "(Intercept)" = "(Intercept)"),
                  width = c(.2, rep(.8/6, 6)), output = "gt") |>
  tab_spanner("Lucid/Cint", columns = 2:4) |> 
  tab_spanner("Bovitz", columns = 5:7) |> 
  tab_style(style = cell_fill(color = "#ffffe0"),
            locations = cells_body(columns = 2:4, rows = 3:4)) |> 
  tab_style(style = cell_fill(color = "#ffffe0"),
            locations = cells_body(columns = 5:7, rows = 3:4)) |> 
  cols_label(
    "(1)" = latex("\\makecell{(1A) \\\\ None \\\\ passed}"),
    "(2)" = latex("\\makecell{(1B) \\\\ Some \\\\ passed}"),
    "(3)" = latex("\\makecell{(1C) \\\\ All \\\\ passed}"),
    "(4)" = latex("\\makecell{(2A) \\\\ None \\\\ passed}"),
    "(5)" = latex("\\makecell{(2B) \\\\ Some \\\\ passed}"),
    "(6)" = latex("\\makecell{(2C) \\\\ All \\\\ passed}")
  ) |> 
  tab_caption(latex("Acquiescence bias and education, by attention check status and survey platform\\label{tbl-by-attention-platform}"))

# Save table to disk
fp_out_t2 <- here("tables", "table_2_results_by_attention_platform.tex")
gtsave(tt_t2, fp_out_t2)

# Load .tex file and implement ad-hoc fixes
lines_out <- readLines(fp_out_t2)
## properly render "<"
lines_out <- lines_out |> str_replace_all("<", latex("$<$"))
## insert \midrule before N
which_n <- str_which(lines_out, "^N \\&")
lines_out <- c(lines_out[1:(which_n-1)], 
               "\\midrule",
               lines_out[which_n:length(lines_out)])

# Save final version of .tex file
cat(sprintf("--Save tables/table_2_results_by_attention_platform.tex (%s)\n",
            f_time_gmt()))
lines_out |> writeLines(fp_out_t2)



# Table C1: main rep results including non-partisans ---------------------------

options(tinytable_latex_placement = "h")

# Estimate models
m_baseline_hr_np <- feols(newdv ~ treatment, hr_raw, cluster = ~r_id)
m_ed_hr_np <- feols(newdv ~ treatment * education_num, hr_raw, cluster = ~r_id)

# Gather models as list
l_m_np <- list(m_baseline_hr_np, m_ed_hr_np)

# Produce regression table using modelsummary
tt_t1_io <- msummary(l_m_np,
                     stars = c('***' = .01, '**' = .05, '*' = .1),
                     coef_map = c("treatment" = "Pos. key",
                                  "treatment:education_num" = "Pos. key × Educ.",
                                  "education_num" = "Educ.",
                                  "(Intercept)" = "(Intercept)"),
                     gof_function = f_gof, gof_map = my_gofs,
                     width = c(.2, rep(.5/2, 2))) |>
  style_tt(i = 1:2, j = 2, background = "#ffffe0") |>
  style_tt(i = 3:4, j = 3, background = "#ffffe0") |>
  style_tt(fontsize = .9)

# Change column names, caption, and handling of "<"
tt_t1_io@names <- c("",
                    "{(1A) \\\\ Baseline}",
                    "{(2A) \\\\ Education int.}")
tt_t1_io@caption <- "Main replication results as in Table \\ref{tbl-main}, but including non-partisan respondents.\\label{tbl-main-io-app}"
tt_t1_io@notes[[1]] <- tt_t1_io@notes[[1]] |> str_replace_all("<", "$<$")

# Save table
cat(sprintf("--Save tables/table_c1_rep_with_nonpartisans.tex (%s)\n",
            f_time_gmt()))
tt_save(tt_t1_io, output = here("tables", "table_c1_rep_with_nonpartisans.tex"), 
        overwrite = T)



# Table C2: acquiescence bias and political awareness --------------------------

# Estimate models
m_pa_cint <- feols(newdv ~ treatment * pol_awareness, 
                   cint, cluster = ~r_id)
m_pa_bovitz <- feols(newdv ~ treatment * pol_awareness, bovitz, cluster = ~r_id)

l_pa <- list("(1) Cint" = m_pa_cint, "(2) Bovitz" = m_pa_bovitz)

# Produce table
tbl_int_pa <- msummary(l_pa,
                       gof_omit = "RMSE|IC|R2$", 
                       stars = c('***' = .01, '**' = .05, '*' = .1), 
                       coef_map = c("treatment:pol_awareness" = "Pos. key × Pol. Awareness",
                                    "pol_awareness" = "Pol. Awareness",
                                    "treatment" = "Pos. key",
                                    "(Intercept)" = "(Intercept)")) |> 
  tinytable::style_tt(i = 1:2, background = "#ffffe0")

# Assign caption
tbl_int_pa@caption <- "Acquiescence bias by political awareness\\label{tbl-by-awareness}"
# Fix issue with "<"
tbl_int_pa@notes[[1]] <- tbl_int_pa@notes[[1]] |> str_replace_all("<", "$<$")

# Save table
cat(sprintf("--Save tables/table_c2_political_awareness.tex (%s)\n",
            f_time_gmt()))
save_tt(tbl_int_pa, 
        output = here("tables", "table_c2_political_awareness.tex"), overwrite = T)



# Table C3: acquiescence bias and response options -----------------------------

# Estimate models

m_unsure_cint <- feols(newdv ~ treatment * education_num,
                       cint[cint$sees_not_sure == 1,],
                       cluster = ~r_id)
m_no_unsure_cint <- feols(newdv ~ treatment * education_num,
                          cint[cint$sees_not_sure == 0,],
                          cluster = ~r_id)
m_unsure_bovitz <- feols(newdv ~ treatment * education_num,
                         bovitz[bovitz$sees_not_sure == 1,],
                         cluster = ~r_id)
m_no_unsure_bovitz <- feols(newdv ~ treatment * education_num,
                            bovitz[bovitz$sees_not_sure == 0,],
                            cluster = ~r_id)
l_m_unsure <- list(m_unsure_cint, m_no_unsure_cint,
                   m_unsure_bovitz, m_no_unsure_bovitz)
                   
# Produce table
                   
tt_unsure <- msummary(l_m_unsure,
                      gof_function = f_gof, gof_map = my_gofs,
                      stars = c('***' = .01, '**' = .05, '*' = .1),
                      coef_map = c("treatment" = "Pos. key",
                                   "treatment:education_num" = "Pos. key × Educ.",
                                   "education_num" = "Educ.",
                                   "(Intercept)" = "(Intercept)"),
                      width = c(.2, rep(.8/4, 4))) |>
  group_tt(j = list("Lucid/Cint" = 2:3, "Bovitz" = 4:5)) |>
  style_tt(i = 1:4, j = 2:5, background = "#ffffe0") |>
  style_tt(fontsize = .85)

# Assign column names and correct issue with "<"
tt_unsure@names <- c("",
                     '{(1A) \\\\ Saw "Not sure"}',
                     '{(1B) \\\\ Did not see "Not sure"}',
                     '{(2A) \\\\ Saw "Not sure"}',
                     '{(2B) \\\\ Did not see "Not sure"}')

tt_unsure@notes[[1]] <- tt_unsure@notes[[1]] |> str_replace_all("<", "$<$")

# Set caption
tt_unsure@caption <- "Acquiescence bias and education, broken down by response options\\label{tbl-by-options}"

# Save table
cat(sprintf("--Save tables/table_c3_response_options.tex (%s)\n",
            f_time_gmt()))
tt_save(tt_unsure, output = here("tables", "table_c3_response_options.tex"), 
        overwrite = T)

